package org.dreamwork.tools.network.bridge.client.fx;

import javafx.stage.Stage;
import org.dreamwork.app.bootloader.ApplicationBootloader;
import org.dreamwork.config.IConfiguration;
import org.dreamwork.config.KeyValuePair;
import org.dreamwork.db.IDatabase;
import org.dreamwork.network.sshd.data.User;
import org.dreamwork.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by seth.yang on 2020/3/9
 */
public class GuiContext {
    private static IConfiguration root;
    private static IDatabase database;
    private static Stage mainStage;
    private static User currentUser;
    public static ResourceBundle error;
    public static ResourceBundle ui;
    public static boolean jar = false;

    private static final Logger logger = LoggerFactory.getLogger (GuiContext.class);

    public static void setConfiguration (IConfiguration config) {
        if (root == null)
            root = config;
        else
            logger.warn ("the root configuration has set, ignore this operate");
    }

    public static IConfiguration getConfiguration () {
        return root;
    }

    public static IConfiguration getConfiguration (String name) {
        return ApplicationBootloader.getConfiguration (name);
    }

    public static IDatabase getDatabase () {
        return database;
    }

    public static void setDatabase (IDatabase database) {
        if (GuiContext.database == null)
            GuiContext.database = database;
        else
            logger.warn ("the database has set, ignore this operate");
    }

    public static Stage getMainStage () {
        return mainStage;
    }

    public static void setMainStage (Stage stage) {
        if (logger.isTraceEnabled ()) {
            logger.trace ("main stage setting to {}", stage);
        }
        GuiContext.mainStage = stage;
    }

    public static User getCurrentUser () {
        return currentUser;
    }

    public static void setCurrentUser (User user) {
        GuiContext.currentUser = user;
    }

    private static final Pattern P = Pattern.compile ("\\$\\{(.*?)\\}");

    public static String getResource (String key, KeyValuePair<?>... params) {
        if (ui == null) {
            return null;
        }

        if (!ui.containsKey (key)) {
            return null;
        }

        String raw = ui.getString (key);
        if (StringUtil.isEmpty (raw)) {
            return raw;
        }

        if (raw.contains ("${")) {
            for (KeyValuePair<?> p : params) {
                if (raw.contains ("${" + p.getName () + "}")) {
                    raw = raw.replace ("${" + p.getName () + "}", String.valueOf (p.getValue ()));
                }
            }
        }

        if (raw.contains ("${")) {
            Matcher m = P.matcher (raw);

            List<KeyValuePair<String>> list = new ArrayList<> ();
            Properties props = System.getProperties ();
            while (m.find ()) {
                String g = m.group (1);
                String v;
                if (ui.containsKey (g)) {
                    v = getResource (g);
                    list.add (new KeyValuePair<> (g, v));
                } else if (props.containsKey (g)) {
                    v = props.getProperty (g);
                    list.add (new KeyValuePair<> (g, v));
                }
            }

            if (!list.isEmpty ()) {
                for (KeyValuePair<String> p : list) {
                    raw = raw.replace ("${" + p.getName () + "}", p.getValue ());
                }
            }
        }

        return raw;
    }

    public static String getResource (String key, String paramName, String paramValue) {
        return getResource (key, new KeyValuePair<> (paramName, paramValue));
    }
}